import { domain } from './config.js';
let tools = {
	// 日期格式处理
	timestamptoData:(time,status)=>{
		if(time.length == 10){
			time = time * 1000;
		}
		let year = new Date(time).getFullYear();
		let month = new Date(time).getMonth() + 1 >= 10 ? new Date(time).getMonth() + 1 : '0' + (new Date(time).getMonth() + 1);
		let date = new Date(time).getDate() >= 10 ? new Date(time).getDate() : '0' + new Date(time).getDate();
		let hour = new Date(time).getHours() >= 10 ? new Date(time).getHours() : '0' + new Date(time).getHours();
		let minutes = new Date(time).getMinutes() >= 10 ? new Date(time).getMinutes() : '0' + new Date(time).getMinutes();
		let second = new Date(time).getSeconds() >= 10 ? new Date(time).getSeconds() : '0' + new Date(time).getSeconds();
		if(status){
			return year + '-' + month + '-' + date + ' ' + hour + ':' + minutes + ':' + second;
		}else{
			return year + '-' + month + '-' + date;
		}
	},
	// 上传图片
	upImg(file){
		uni.showLoading({
			title: '上传中...'
		})
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: domain + "/api/common/upload",
				// url: getApp().globalData.domain + "/api/common/uploadOne",
				filePath:file,
				name:'file',
				formData: {
				  token:uni.getStorageSync('uToken')?uni.getStorageSync('uToken'):'',
				},
				success:(res)=>{
					uni.hideLoading();
					res = JSON.parse(res.data);
					if (res.code == '1') {
						resolve(res.data);
					} else {
						resolve(false);
						uni.showToast({
							title:res.msg,
							icon:"none"
						})
					}
				},
				fail:(err)=>{
					uni.hideLoading();
					console.log(err);
					uni.showToast({
						title:'图片上传失败',
						icon:"none"
					})
				}
			})
		})
	},
	// 获取下标
	getIdx(arr,sx,val){
		let idx = arr.findIndex((line)=>{
			return line[sx] == val;
		})
		return idx!=-1?idx:'-1';
	},
}
export default tools;